/**
 * Created by lwy on 2017/6/20.
 */

//$('li').click(function(){
//    $(this).children().find('i').show();
//    $(this).siblings().children().find('i').hide();
//})
var payChanl;

$(document).ready(function(){

    //判断是否是微信公众号浏览器
    var ua = navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i)=="micromessenger")
    {
        payChanl=0;
        $('#pingxx_wx_pub').show();//显示微信支付
    }
    else{
        payChanl=1;
        $('#aliPay').show();//显示支付宝支付
    }



    var open_id=GetQueryString('openid')==null?getItem("open_id"):GetQueryString('openid');
    if(open_id!=null){
        setItem("open_id",open_id);
        $('#money').val(GetQueryString('money'));
        if( $('#money').val()!=""){
            pingxx_wx_pub_pay();
        }
        //pingxx_wx_pub_pay();
    }
});



//返回到首页
function back() {
    window.location.href = '/m/html/index.html';
}
function showModal(payChannel){
    var url ='/ali/pay.do?token='+getItem('token')+'&money='+$('#money').val()+'&mark=8';
    location.href=url;
}
//微信公众号支付
function pingxx_wx_pub_pay(code){
    var orderCode;
    if(code!=null||code!=""){
        orderCode=code;
    }else {
        orderCode=GetQueryString('code')
    }

    var money=GetQueryString('money');
    if(money==null){
        money= $('#money').val()
    }
    var open_id=GetQueryString('openid')==null?getItem("open_id"):GetQueryString('openid');
    $.ajax({
        url:'/wechat/pay.json',
        dataType:'json',
        data:{
            token:getItem('token'),
            code:"",
            openid:open_id,
            t:new Date()
        },
        success:function(data){
            if(data.code==10){
                window.location.href=data.url;
                return;
            }
            if(data.code==0){
                mui.toast(data.msg)
            }
         if(data.code==1){
                 if (typeof WeixinJSBridge == "undefined"){
                     if( document.addEventListener ){
                         document.addEventListener('WeixinJSBridgeReady', onBridgeReady(data,appid,data.timeStamp,data.nonceStr,data.packageValue,data.paySign), false);
                     }else if (document.attachEvent){
                         document.attachEvent('WeixinJSBridgeReady', onBridgeReady(data,appid,data.timeStamp,data.nonceStr,data.packageValue,data.paySign));
                         document.attachEvent('onWeixinJSBridgeReady', onBridgeReady(data,appid,data.timeStamp,data.nonceStr,data.packageValue,data.paySign));
                     }
                 }else{
                     WeixinJSBridge.invoke(
                         'getBrandWCPayRequest',{
                             "appId" : data.appid,     //公众号名称，由商户传入
                             "timeStamp": data.timeStamp,         //时间戳，自1970年以来的秒数
                             "nonceStr" : data.nonceStr, //随机串
                             "package" : data.packageValue,
                             "signType" : "MD5",         //微信签名方式:
                             "paySign" : data.paySign    //微信签名
                         },function(res){
                             if(res.err_msg == "get_brand_wcpay_request:ok"){
                                 mui.toast("微信支付成功!");
                                 window.location.href = '/m/html/personal_v2.html';
                             }else if(res.err_msg == "get_brand_wcpay_request:cancel"){
                                 mui.toast("用户取消支付!");
                                 window.location.href = '/m/html/personal_v2.html';
                             }else{
                                 mui.toast("支付失败!");
                                 window.location.href = '/m/html/personal_v2.html';
                             }
                         });
                 }
         }
        }
    })
}
function onBridgeReady(appid,timeStamp,nonceStr,package,paySign){
    WeixinJSBridge.invoke(
        'getBrandWCPayRequest', {
            "appId" : appid,     //公众号名称，由商户传入
            "timeStamp": timeStamp,         //时间戳，自1970年以来的秒数
            "nonceStr" : nonceStr, //随机串
            "package" : package,
            "signType" : "MD5",         //微信签名方式:
            "paySign" : paySign   //微信签名
        },function(res){
            if(res.err_msg == "get_brand_wcpay_request:ok"){
                mui.toast("微信支付成功!");
            }else if(res.err_msg == "get_brand_wcpay_request:cancel"){
                alert("用户取消支付!");
            }else{
                alert("支付失败!");
            }
        });
}

//给微信支付增加监听事件

 var submit = document.getElementById("submit");
submit.addEventListener('tap',function(){

    $.ajax({
        url:'/accountC/getAccount.json',
        dataType:'json',
        data:{
            token:getItem('token'),
        },
        type:'post',
        success:function (data) {
            if(data.code==-1){
                window.location.href="login.html"
            }
            if(data.code==0){
                mui.toast(data.msg);
                return;
            }
            if(data.code==1){
                if(data.datas.account.accountStateValue==1){
                    if($('#money').val()==''){
                        mui.toast('金额不能为空');
                        return;
                    }
                    if($('#money').val()<100){
                        mui.toast('充值金额不能小于100');
                        return;
                    }
                    if($('#money').val()>1000000){
                        mui.toast('充值金额不能大于一百万');
                        return;
                    }

                    if(payChanl==0){
                        mui.toast("点击了微信支付！");
                        pingxx_wx_pub_pay();
                    }else if(payChanl==1){
                        showModal(8);
                    }
                }else{
                    mui.alert('当前用户已无效');
                    return;
                }
            }
        }
    })

 });


////给支付宝增加监听事件
//var submit = document.getElementById('submit');
//submit.addEventListener('tap',function(){
//    showModal(8);
//});
//给微信公众号支付增加监听事件
/*var pingxx_wx_pub = document.getElementById("pingxx_wx_pub");
pingxx_wx_pub.addEventListener('tap',function(){

    pingxx_wx_pub_pay();
});*/


//删除
$('#itemBox').on('tap', '.mui-btn', function(event) {
    var elem = this;
    var li = elem.parentNode.parentNode;
    var id = $(this).parent().parent().attr('id');
    if(!ifCyber()){
        mui.toast('请检查网络');
    }
    $.ajax({
        url:'/rechargeC/delOrder.json',
        dataType:'json',
        data:{
            id:id,
        },
        type:'post',
        success: function (data) {
            if(data.code==1){
                li.parentNode.removeChild(li);
                if($('#itemBox li').length==0){
                $('<div class="kong"><img src="../img/kong.png" alt="暂无数据"></div>').appendTo($('#itemBox'));
                }
            }
        }
    })
});

//请求是否有待支付
$(function(){
    $.ajax({
        url:'/rechargeC/queryCreate.json',
        dataType:'json',
        data:{
            token:getItem('token'),
        },
        type:'post',
        success: function (data) {
            if(data.code==1&&data.datas.length>0){
                //$('#waitPay').show();
                //$('.mui-title ul li').css({'width':'50%','text-decoration':'underline'});
                //$('#onlinePay').addClass('active');
                for(var i=0;i<data.datas.length;i++){
                    var n = i%2;
                    var imgStyle = '';
                    if(n==0){
                        imgStyle = 'margin-right:16%';
                    }else{
                        imgStyle = 'margin-right:65%';
                    }
                    $('<li class="mui-table-view-cell" id="'+data.datas[i].id+'">'+
                        '<img src="../img/waitPay-bg.png" alt="img" class="moneyBg" style="'+imgStyle+'">'+
                    '<div class="mui-slider-right mui-disabled">'+
                    '<a class="mui-btn mui-btn-red">删除</a></div>'+
                    '<div class="mui-slider-handle">'+
                    '<div class="waitMoneyBox"><span id="waitmoney">￥'+data.datas[i].money+'</span></div>'+
                    '<div class="codeBox"><span id="code">'+data.datas[i].code+'</span></div>'+
                    '<div class="timeBox"><span>超过2小时自动关闭</span><span class="goPay" money="'+data.datas[i].money+'" code="'+data.datas[i].code+'"></span></div>'+
                    '</div></li>').appendTo($('#itemBox'))
                }
            }else{
                $('<div class="kong"><img src="../img/kong.png" alt="暂无数据"></div>').appendTo($('#itemBox'));
                //$('#onlinePay').css({'width':'100%','text-decoration':'none','pointer-events': 'none'});
                //$('#onlinePay').removeClass('active');
                //$('#waitPay').hide();
            }
        }
    })
})

$('.mui-title ul li').click(function(){
    $(this).addClass('active');
    $(this).find('.active-border').show();
    $(this).siblings().removeClass('active');
    $(this).siblings().find('.active-border').hide();
})

$('#onlinePay').click(function(){
    $('#onlineBox').show();
    $('#waitBox').hide();
})

$('#waitPay').click(function(){
    $('#onlineBox').hide();
    $('#waitBox').show();
})

//去支付
mui('body').on('tap','.goPay',function(){
    var money = $(this).attr('money');
    var code = $(this).attr('code');

    if(payChanl==0){
        pingxx_wx_pub_pay(code);
    }else if(payChanl==1){
        var url ='/ali/pay.do?token='+getItem('token')+'&money='+money+'&code='+code;
        location.href=url;
    }

})
